#  -*- coding: utf-8 -*-
# 将当前目录下所有 .md结尾的文件移动到 md 目录
import os
import shutil
from loguru import logger
from sys import exit
def rename_file(src,dst):
    """

    :param src:
    :param dst:
    :return:
    """
    try:
        shutil.copy2(src,dst)
        logger.info(f"{src} -> {dst} Success")
    except Exception as e:
        logger.error(e)
        logger.error(f"{src} -> {dst} Failed")
        # exit(2)
def get_title(file):
    """

    :param file:
    :return:
    """
    # 通过读取 .md文件的第二行获取标题
    with open(file, "r", encoding="utf-8") as f:
        lines = f.readlines()
        title_ = lines[1].replace("title: ", "")
    return title_
if __name__ == '__main__':
    # 获取当前目录下.md结尾的文件
    md_files = [i for i in os.listdir("md") if i.endswith(".md")]
    for md_file in md_files:
        md_file = os.path.join("md",md_file)
        title = get_title(md_file).strip()
        logger.info(title)
        new_name = title + ".md"
        dst_ = os.path.join("mdFile",new_name)
        dst_ = os.path.abspath(dst_)
        md_file = os.path.abspath(md_file)
        logger.info(f"Src: {md_file} - Dst: {dst_}")
        rename_file(md_file,dst_)

